package com.dyzs.activity.word;

import java.util.HashMap;
import java.util.Map;

import android.content.ContentValues;
import android.database.Cursor;
import android.os.Bundle;
import android.text.Html;
import android.text.method.LinkMovementMethod;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.dyzs.activity.CommonActivity;
import com.dyzs.activity.R;
import com.dyzs.client.WordClient;
import com.dyzs.util.Constants;
import com.dyzs.util.DatabaseOperation;

public class WordDetailActivity extends CommonActivity  {
	private Map map; 
	
	private String wordId;
	private String cityCode;
	private String showType;
	
	Button bt;
	private boolean isSave=false;
	
	private DatabaseOperation dbOperator;
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.word_detail);
		this.setTitle("导游词");	
		bt=(Button)this.findViewById(R.id.word_detail_bt);
		dbOperator=new DatabaseOperation(this);	
		
		
		wordId=getIntent().getStringExtra("wordId");		
		showType=getIntent().getStringExtra("showType");
		if("1".equals(showType)){
			cityCode=getIntent().getStringExtra("cityCode");
			update(wordId);	
			
		}else if("2".equals(showType)){
			isSave=true;
			Cursor cursor =dbOperator.query("word", new String[]{"title","sig_name","scen_name","word_content","city_id"}, "word_id=? ",  new String[]{wordId}, null, null, null,null);
			cursor.moveToFirst();
			map=new HashMap();
			map.put("sig_name", cursor.getString(cursor.getColumnIndex("sig_name")));
			map.put("scen_name", cursor.getString(cursor.getColumnIndex("scen_name")));
			map.put("content", cursor.getString(cursor.getColumnIndex("word_content")));
			map.put("title", cursor.getString(cursor.getColumnIndex("title")));
			cityCode= cursor.getString(cursor.getColumnIndex("city_id"));
			dbOperator.closeDb(cursor);	
			showDetail();			
		}		
		
	}

	private void update(final String wordId) {
		showProgressDialog(getString(R.string.search_progress_title),getString(R.string.search_progress_content), true, true);
		new Thread(new Runnable() {
			public void run() {
				try {
					getDataFromService(wordId);
					WordDetailActivity.this.runOnUiThread(new Runnable() {
						public void run() {
							proDialog.dismiss();
							showDetail();
						}						
					});
				} catch (Exception e) {
					onException(e);
				}
			}
		}).start();
	}

	public void onException(final Exception e) {
		WordDetailActivity.this.runOnUiThread(new Runnable() {
			public void run() {
				if (proDialog != null) {
					proDialog.dismiss();
				}				
			}
		});
	}

	private void getDataFromService(String wordId) throws Exception {
		WordClient trainMDDClient = new WordClient();
		String x = trainMDDClient.updateDetail(wordId);
		map= trainMDDClient.pullDetailXml(x);		
	}
	private void showDetail() {
		if(map.get("sig_name").equals(""))
			((TextView)findViewById(R.id.word_detail_where)).setText(map.get("scen_name")+"");
		else
			((TextView)findViewById(R.id.word_detail_where)).setText(map.get("scen_name")+"---"+map.get("sig_name"));
		((TextView)findViewById(R.id.word_detail_title)).setText(map.get("title").toString());
		
		
		
//		WebView wv=(WebView)findViewById(R.id.word_detail_content);
//		wv.setBackgroundColor(0); 
//				
//		String content=map.get("content").toString().replace("^", "<");	
//		try {
//        	wv.loadDataWithBaseURL (null, "<font color=#FFFFFFFF>"+content+"</font>", "text/html", "utf-8",null);				
//		} catch (Exception e) {
//			e.printStackTrace();
//		}	
		
		TextView tv=(TextView)findViewById(R.id.word_detail_content);
		tv.setText(Html.fromHtml(map.get("content").toString().replace("^", "<")));
		tv.setMovementMethod(LinkMovementMethod.getInstance());
//		
//		((TextView)findViewById(R.id.word_detail_content)).setText(map.get("content").toString().replace("^", "<"));
		
		
		
		
		
		if("1".equals(showType)){
			Cursor cursor =dbOperator.query("word",  new String[]{"count(word_id) num"}, "word_id=? ",  new String[]{wordId}, null, null, null,null);
			cursor.moveToFirst();
			int k = cursor.getInt(cursor.getColumnIndex("num"));
			if(k>0)
				isSave=true;		
			dbOperator.closeDb(cursor);				
			if(isSave){//更新
				bt.setText("取消收藏");
			}else{
				bt.setText("收藏");
			}
		}else{
			bt.setText("取消收藏");
		}
		
	}
	
	public void onClick(View v){
		if(v.getId()==R.id.word_detail_bt){
			ContentValues values = new ContentValues();
			values.put("word_id", wordId);
			values.put("title", map.get("title").toString());
			values.put("sig_name", map.get("sig_name").toString());
			values.put("scen_name", map.get("scen_name").toString());
			values.put("word_content", map.get("content").toString());
			values.put("city_id", cityCode);
			values.put("user_id", Constants.USER_Mobile);
			
			try {
				
					if(bt.getText().toString().equals("收藏")){
						dbOperator.insert( "word", values);
						bt.setText("取消收藏");
					}else {
						dbOperator.del("word", "word_id=? and user_id=?", new String[] {wordId,Constants.USER_Mobile});					
						bt.setText("收藏");
					}				
			} catch (Exception e) {
				onException(e);
			}
			
			
		}else if(v.getId()==R.id.word_detail_esc){
			this.finish();
		}		
	}

}
